package com.ldy.core.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.core.common.Result;
import com.ldy.core.entity.BaseRole;
import com.ldy.core.entity.BaseRoleMenu;
import com.ldy.core.service.BaseRoleMenuService;
import com.ldy.core.service.BaseRoleService;
import com.ldy.core.vo.RoleVO;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class RoleController {


	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@Autowired
	private BaseRoleService roleService;
	
	@Autowired
	private BaseRoleMenuService roleMenuService;

	/**
	 * 列表分页查询
	 * @param current
	 * @param size
	 * @param role
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, String role) {
		Page<BaseRole> page = new Page<BaseRole>(current, size);
		QueryWrapper<BaseRole> query = new QueryWrapper<>();
		if (StringUtils.hasText(role)) {
			query.like("role", role);
		}
		query.orderByDesc("create_time");
		return Result.success(roleService.page(page, query));
	}
	
	/**
	 * 列表查询
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList() {
		return Result.success(roleService.list());
	}
	
	/**
	 * 查看角色菜单
	 * @return
	 */
	@PostMapping("/menu/selectList")
	public Result selectListMenu(String roleId) {
		QueryWrapper<BaseRoleMenu> query = new QueryWrapper<>();
		query.eq("role_id", roleId);
		return Result.success(roleMenuService.list(query));
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 */
	@PostMapping("/save")
	public Result save(@RequestBody RoleVO role) {
		logger.info("method: {}, params:[role: {}]", "insert", JSONObject.toJSONString(role));
		roleService.saveHandle(role);
		return Result.success();
	}

	
	/**
	 * 删除
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(String id) {
		logger.info("method: {}, params:[id: {}]", "delete", id);
		roleService.removeById(id);
		return Result.success();
	}
}
